from datetime import datetime
from qturboframe.lib.utils.by import By
from qturboframe.lib.utils.controlname import ControlName
from qturboframe.lib.report.log import logger
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.ranorex.win_control.combo_box import ComboBox as WinComboBox
from qturboframe.public_ui.ucf_wpf.basecontrol import BaseControl
from qturboframe.public_ui.ucf_wpf.button import Button as UcfButton
from qturboframe.public_ui.ucf_wpf.context_menu import ContextMenu
from qturboframe.lib.utils.common import Common
from qturboframe.lib.utils.CustomError.custom_error import CustomError


class Combobox(BaseControl):
    """
    ucf wpf组合框（即下拉框）

    :Attributes:
     - _driver: 控件所在的当前Control对象
     - _control: 控件元素Control对象
     - combobox: 当前的combobox对象
     - timeout: 步骤操作的超时时间

     :Raises
        CsClientError 没有找到控件异常

    :Example:
     - 例1: Combobox({By.TEXT: '下拉框'})
     - 例2: Combobox({By.AUTOMATIONID:'automationid'})
     - 例3: Combobox(control)
     - 例4: Combobox({By.TEXT: 'Click'}, father=client)
     - 例5: Combobox({By.AUTOMATIONID:'automationid'}, father=client)
     - 例6: Combobox(control, father=client)
    """

    def __init__(self, element_para, father=None, timeout=10000, index=1):
        if isinstance(element_para, dict) and By.LABEL_TEXT in element_para:
            text = element_para.get(By.LABEL_TEXT)
            text = Common.update_special_character(text)
            xpath = ".//combobox//text[@text~'{}']".format(text, text)
            controls = father.find_elements_by_attribute(By.XPATH, xpath, timeout=timeout)
            if controls:
                control = controls[index-1]
                xpath = ".//ancestor::combobox"
                combobox_control = control.find_element_by_attribute(By.XPATH, xpath, timeout=timeout)
                super().__init__(combobox_control, father, timeout)
            else:
                raise CsClientError(CustomError.Error_EQ03001000_Cannot_Find_Element,"没有找到此控件:{}".format(element_para))
        else:
            super().__init__(element_para, father, timeout)

        if self._control.get_control_name() == ControlName.COMBOBOX:
            self.combobox = WinComboBox(self._control.control)
        else:
            raise CsClientError(CustomError.Error_EQ03001004_InCorrect_Type,"请确定查找的控件是combobox控件")

    def single_select(self, value):
        """
        wpf ucf 组合框选择下拉内容 - 单选

        :Args:
         - value: 选择项的文本

        :Raise:
         - CsClientError 当选择的文字不在下拉框中时

        :Example:
         - 例1: combobox.signle_select('ghsfg 2')
        """
        #点击向下箭头
        dropdown_button_xpath = ".//button[@automationid='PART_DropDownToggle']//element[@automationid='BtnArrowBackground']"
        UcfButton({By.XPATH: dropdown_button_xpath}, self.combobox, timeout = self.timeout).click()

        ContextMenu(father=self._driver, timeout=self.timeout).single_select(value)

    def multi_select(self, select_value):
        """
        wpf ucf 组合框选择下拉内容 - 多选

        :Args:
         - select_value: 选择项的文本， 多个文本时用逗号(,)分开

        :Raise:
         - CsClientError 当选择的文字不在下拉框中时

        :Example:
         - 例1: combobox.multi_select('ghsfg 2,sfhhtrt 3')
        """
        #点击向下箭头
        dropdown_button_xpath = ".//button[@automationid='PART_DropDownToggle']//element[@automationid='BtnArrowBackground']"
        dropdown_button = UcfButton({By.XPATH: dropdown_button_xpath}, self.combobox, timeout = self.timeout)
        dropdown_button.click()

        ContextMenu(father=self._driver, timeout=self.timeout).multi_select(select_value)

        #使多选下拉框缩回
        dropdown_button.click()
